﻿using System;


namespace CoreEx.Common.Cache
{
    /// <summary>
    /// Represents a dictionary of cached objects that can be retrieved by its key.
    /// </summary>
    /// <typeparam name="TKey">The type of keys in the cache.</typeparam>
    /// <typeparam name="TValue">The type of values in the cache.</typeparam>
    public interface ILazyCache<TKey,TValue> : ILifeSpan
    {        
        
        /// <summary>
        /// Returns the cached value by its <paramref name="key"/>.
        /// </summary>
        /// <param name="key">The key used to locate the cached object.</param>
        /// <param name="initializer">The <see cref="Func{TResult}"/> delegate that is executed if the cached object is not valid.</param>
        /// <returns><typeparamref name="TValue"/></returns>
        TValue GetValue(TKey key, Func<TKey, TValue> initializer);
       
        /// <summary>
        /// Invalidates the cached object and causes the initializer delegate 
        /// passed to the <see cref="GetValue"/> method to execute.
        /// <param name="key">The key used to locate the cached object.</param>
        /// </summary>
        void Invalidate(TKey key);

        /// <summary>
        /// Invalidates all cached objects and causes the initializer delegate 
        /// passed to the <see cref="GetValue"/> method to execute.
        /// </summary>
        void Invalidate();
    }
}
